package weather;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


/**
 * 主要调用Web服务的类
 */
public class WeatherClient {
    public String[] getWeather(String cityCode, String userID) {
        String[] weatherData = new String[100]; // 假设最多有 100 条数据
        String urlString = "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather?theCityCode=" + cityCode + "&theUserID=" + userID;

        try {
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // 处理响应
            // 假设返回的响应是以字符串形式存储在 response 中
            String[] results = response.toString().split("<string>"); // 按照 <string> 分割
            for (int i = 1; i < results.length; i++) {
                weatherData[i - 1] = results[i].split("</string>")[0]; // 提取字符串内容
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return weatherData;
    }
}
